package com.ecube.maintenance.components.application;

import android.app.Application;

import com.ecube.maintenance.R;
import com.ecube.maintenance.biz.apis.impl.CarBrandAction;
import com.ecube.maintenance.biz.apis.impl.CityAction;
import com.ecube.maintenance.biz.apis.impl.OrderAction;
import com.ecube.maintenance.biz.commons.Constants;
import com.ecube.maintenance.biz.commons.ICCallBack;
import com.ecube.maintenance.biz.commons.ICTException;
import com.ecube.maintenance.biz.commons.IListItem;
import com.ecube.maintenance.biz.commons.VolleySingleton;
import com.ecube.maintenance.biz.network.NetWorkEvent;
import com.ecube.maintenance.biz.network.SimpleAsynTask;
import com.ecube.maintenance.managers.LocationBDManager;
import com.ecube.maintenance.managers.PrefManager;
import com.ecube.maintenance.managers.SDCardManager;
import com.ecube.maintenance.pojos.AddressInfo;
import com.ecube.maintenance.pojos.CarPropertyListInfo;
import com.ecube.maintenance.pojos.CityListInfo;
import com.ecube.maintenance.pojos.OrderInfo;
import com.ecube.maintenance.utils.Bog;
import com.ecube.maintenance.utils.DeviceUtil;
import com.ecube.maintenance.utils.GsonUtil;

import java.util.List;

/**
 * Created by Trible Chen on 2014/9/6.
 */
public class CCBApplication extends Application {


    public static AddressInfo sAddressInfo;
    String key;

    public static void setsAddressInfo(AddressInfo addressInfo) {
        sAddressInfo = addressInfo;
    }

    public static void setsAddressInfoByCityInfo(CityListInfo.CityInfo cityInfo) {
        if (sAddressInfo == null) {
            sAddressInfo = new AddressInfo();
        }
        sAddressInfo.setLon(cityInfo.getLon());
        sAddressInfo.setLat(cityInfo.getLat());
        sAddressInfo.setAddress(cityInfo.getName());
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initSystems();
        testUserActionAPIs();
        testSellerActionAPIS();
        //testOrderAPIs();
        getLocationData();
        testCityData();
        testCarBrandData();
    }

    void initSystems() {
        String name = getString(R.string.app_name);
        Bog.init(this, name);
        PrefManager.initPrefManager(this, name);
        SDCardManager.initStorageWithClassicPaths(this, name);
        DeviceUtil.init(this);
        VolleySingleton.init(this);
        LocationBDManager.init(this);
    }

    void testUserActionAPIs() {

//        UserAction.fetchValidCodes("15882003904",new ICCallBack() {
//            @Override
//            public void done(Object result, ICTException exception) {
//                if ( exception == null ){
//                    Double k = ((StringMap<Double>)result).get("key");
//                    key = k.intValue()+"";
//                    UserAction.logins("15882003904",key,new ICCallBack<UserInfo>() {
//                        @Override
//                        public void done(UserInfo result, ICTException exception) {
//                            if ( exception == null ){
//                                Bog.toast("success");
//                            } else {
//                                Bog.toast(exception.getMessage());
//                            }
//                        }
//                    });
//                } else {
//                    key = null;
//                    Bog.toast(exception.getMessage());
//                }
//            }
//        });

//        UserAction.logouts("",new ICCallBack(){
//            @Override
//            public void done(Object result, ICTException exception) {
//                if ( exception == null ){
//                } else {
//                    Bog.toast(exception.getMessage());
//                }
//            }
//        });
//        UserAction.updates(null,new ICCallBack() {
//            @Override
//            public void done(Object result, ICTException exception) {
//
//            }
//        });
    }

    void testSellerActionAPIS() {
//        SellerAction.fetchSellerList("",new AddressInfo(),0,"","","","",new ICCallBack<SellerListInfo>() {
//            @Override
//            public void done(SellerListInfo result, ICTException exception) {
//                if ( exception == null ){
//
//                } else {
//                    exception.printStackTrace();
//                }
//            }
//        });

//        SellerAction.fetchSellerDetail("","","",new ICCallBack<SellerInfo>() {
//            @Override
//            public void done(SellerInfo result, ICTException exception) {
//                if ( exception == null ){
//
//                } else {
//                    exception.printStackTrace();
//                }
//            }
//        });
//        SellerAction.collectSeller("","",0,new ICCallBack() {
//            @Override
//            public void done(Object result, ICTException exception) {
//                if ( exception == null ){
//
//                } else {
//                    exception.printStackTrace();
//                }
//            }
//        });
//        SellerAction.claimProduct("","",new AddressInfo(),new ICCallBack() {
//            @Override
//            public void done(Object result, ICTException exception) {
//                if ( exception == null ){
//
//                } else {
//                    exception.printStackTrace();
//                }
//            }
//        });
    }

    void testOrderAPIs() {
//        OrderAction.submit(null, null, null, null, new ICCallBack<OrderInfo>() {
//            @Override
//            public void done(OrderInfo result, ICTException exception) {
//                if (exception == null) {
//
//                } else {
//                    exception.printStackTrace();
//                }
//            }
//        });
//
//        OrderAction.fetchOrderDetails("", "", new ICCallBack<OrderInfo>() {
//            @Override
//            public void done(OrderInfo result, ICTException exception) {
//                if (exception == null) {
//
//                } else {
//                    exception.printStackTrace();
//                }
//            }
//        });
//
//        OrderAction.cancelOrder("", "", new ICCallBack<OrderInfo>() {
//            @Override
//            public void done(OrderInfo result, ICTException exception) {
//                if (exception == null) {
//
//                } else {
//                    exception.printStackTrace();
//                }
//            }
//        });
//        OrderAction.fetchOrderList("", "", new ICCallBack<List<OrderInfo>>() {
//            @Override
//            public void done(List<OrderInfo> result, ICTException exception) {
//                if (exception == null) {
//
//                } else {
//                    exception.printStackTrace();
//                }
//            }
//        });

    }

    void getLocationData() {
        AddressInfo info = GsonUtil.getInfosFromJson(
                PrefManager.getInstance().getString(Constants.PRF_CURRENT_CITY)
                , AddressInfo.class);
        CityAction.getStaticCityList(this);
//        成都","lon":104.067923,"lat":30.679943
        if (info == null) {
            sAddressInfo = new AddressInfo();
            sAddressInfo.setAddress("成都");
            sAddressInfo.setLat(30.679943);
            sAddressInfo.setLon(104.067923);
            LocationBDManager.requestLocation(new LocationBDManager.MyBDLocationListener() {
                @Override
                protected void onReceive(AddressInfo info) {
                    if (info != null) {
                        sAddressInfo = info;
                        String s = GsonUtil.toString(info, AddressInfo.class);
                        PrefManager.getInstance().putString(Constants.PRF_CURRENT_CITY, s);
                    } else {
                        Bog.toast("定位失败");
                    }
                }
            });
        } else {
            sAddressInfo = info;
        }
    }

    void testCityData() {
//        List<IListItem<CityListInfo.CityInfo>> citydata = CityAction.getStaticCityList(this);
    }

    void testCarBrandData() {
        new SimpleAsynTask().doTask(new SimpleAsynTask.AsynTaskListner() {
            @Override
            public void doInBackground() {
                CarBrandAction.getCarBrands(CCBApplication.this);
            }
            @Override
            public void onTaskDone(NetWorkEvent event) {
            }
        });
//        List<IListItem<CarPropertyListInfo.BaseCarPropertyList.BaseCarProperty>>
//                serials = CarBrandAction.getCarSerialsByBrandId(17);
//        List<IListItem<CarPropertyListInfo.BaseCarPropertyList.BaseCarProperty>>
//                models = CarBrandAction.getCarModelsByBrandIdAndSerialId(17,147);

    }
}
